<?php


namespace app\lmsx\admin;

use app\common\builder\ZBuilder;
use app\lmsx\model\Datum as DatumModel;
use app\lmsx\model\Category as CategoryModel;
use think\Db;

/**
 * 资料库
 * @author lan
 */
class Datum extends Base
{
    public function index(){
        // 获取查询和排序规则
        $map = $this->getMap();
        $order = $this->getOrder();
        $data = Db::table('dp_lmsx_datum')
            ->where($map)
            ->order($order)
            ->order('id desc')
            ->paginate()->each(function($item){
                $arr=['0'=>'暂无','1'=>'衡阳','2'=>'株洲','3'=>'岳阳'];
                $item['position']=$arr[$item['position']];
                // 对分页数据可以做额外处理
                return $item;
            });
        return ZBuilder::make('table')
            ->setTableName('lmsx_datum')  //需要修改 这里修改正确可以直接修改字段 删除
            ->addColumns([
                ['id','id'],
                ['title','资料名称'],
                ['img','资料预览图片','pictures'],
                ['position','城市'],
                ['grade','年级'],
                ['subject','科目'],
                //['view','浏览量'],
                ['create_time','创建时间','datetime'],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButton('add', [],[])
            ->addRightButton('edit',[],[])
            ->addTopButtons(['delete'])
            ->setRowList($data)
            ->addFilter(['grade','subject'])
            ->addFilter(['position'],['1'=>'衡阳','2'=>'株洲','3'=>'岳阳'])
            ->setSearch(['id' => 'ID', 'title' => '资料名称'])
            ->fetch();
    }
    public function add(){
        if($this->request->isPost()){
            // 第一步 获取数据 如果后台面向大众 需要用only方法
            $data = request()->post();
            // 第二部 验证数据
            $result = $this->validate($data,'Datum');
            if($result!==true) $this->error($result);
            //dump($data);die;
            // 第三部 存储
            DatumModel::create($data);
            $this->success('保存成功', null, '_parent_reload');
        }

        return ZBuilder::make('form')
            ->addFormItems([
                ['text', 'title', '资料名称'],
                ['images', 'img', '资料预览图片','必须,限5张,每张图片大小不能超过2M'],
                ['file', 'document', '资料文档','','','','doc,docx,ppt,pptx,wps'],
                ['linkage','grade','年级','',CategoryModel::getGrade(0),'',url('get_subject'),'subject'],
                ['radio', 'position', '城市','',['1'=>'衡阳','2'=>'株洲','3'=>'岳阳'],1],
                ['select','subject','科目'],
                //['number', 'view', '浏览量']
            ])
            ->setTrigger('grade', '初一,初二,初三', 'position')
            ->fetch();
    }

    public function get_subject($grade='',$t=false){
        $arr['code']='1';
        $arr['msg']='请求成功';
        $row=Db::table('dp_lmsx_category')->where('name',$grade)->field('cid')->find();
        $ke=[];
        $row['cid']=explode(",",$row['cid']);
        foreach ($row['cid'] as $v){
            $ke[$v]=$v;
        }
        $arr['list']=format_linkage($ke);
        if($t==true){
            $arr['list']=$ke;
        }
        return $arr;
    }

    public function edit($id=null){
        if($this->request->isPost()){
            $data = request()->post();
            $result = $this->validate($data,'Datum');
            if($result!==true) $this->error($result);
            if(($data['grade']=='高一')||($data['grade']=='高二')||($data['grade']=='高三')){
                $data['position']=0;
            }
            //dump($data);die;
            DatumModel::update($data);
            $this->success('保存成功', null, '_parent_reload');
        }

//        $data = DatumModel::get($id);
        $data=Db::table('dp_lmsx_datum')->where('id',$id)->find();
        //dump($data);die;
        $m=$this->get_subject($data['grade'],true);
//        $z=$m['list'];
        //dump($data);die;
        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden','id'],
                ['text', 'title', '资料名称','必须'],
                ['images', 'img', '资料预览图片','必须,限5张,每张图片大小不能超过2M'],
                ['file', 'document', '资料文档','选填','','','','doc,docx,ppt,pptx,wps'],
                ['linkage','grade','年级','',CategoryModel::getGrade(0),'','','subject'],
                ['select','subject','科目','',$m['list']],
//                ['select','grade','年级','',CategoryModel::getGrade(0)],
                 ['radio', 'position', '城市','年级为初中，必选',['1'=>'衡阳','2'=>'株洲','3'=>'岳阳']],

                //['number', 'view', '浏览量']
            ])
//            ->setTrigger('grade', '初一,初二,初三', 'position')
            ->setFormData($data)
            ->fetch();
    }
}